<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Layui</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="/static/layui/css/layui.css"  media="all">
  <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
           
	<div class="demoTable" style="margin:10px">
	  用户名：
	  <div class="layui-inline">
	    <input class="layui-input" name="id" id="demoReload" autocomplete="off">
	  </div>
	  <button class="layui-btn" data-type="reload">搜索</button>
	  <button class="layui-btn" id="btn-add">新增</button>
	  <button class="layui-btn layui-btn-danger" id="btn-lock">冻结</button>
	  <button class="layui-btn layui-btn-normal" id="btn-unlock">解冻</button>
	</div>
	 
	<table class="layui-hide" id="LAY_table_user" lay-filter="user"></table> 
               
              
<script type="text/html" id="sexTpl">
  {{#  if(d.sex === 2){ }}
    <span style="color: #F581B1;">女</span>
  {{#  } else { }}
           男
  {{#  } }}
</script>
<script type="text/html" id="statusTpl">
  {{#  if(d.status === 1){ }}
    <span style="color: #0000ff ;">可用</span>
  {{#  } else if(d.status === 2) { }}
    <span style="color: #ccc;">不可用</span>
  {{#  } else { }}
           待激活
  {{#  } }}
</script>
<script type="text/html" id="barDemo">
  <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>        
<script src="/static/layui/layui.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->

 
<script>
layui.use('table', function(){
	  var table = layui.table;
	  //方法级渲染
	  table.render({
		 elem: '#LAY_table_user'
	    ,url:'/user/dataList'
	    ,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
	    ,cols: [[
	      {type:'checkbox', fixed: 'left'}
	      ,{field:'id', title: 'ID', sort: true}
	      ,{field:'username',title: '用户名'}
	      ,{field:'sex', title: '性别', sort: true,templet: '#sexTpl'}
	      ,{field:'status',title: '状态',templet: '#statusTpl'}
	      ,{field:'remark', title: '备注', }
	      ,{field:'create_time', title: '创建时间'}
	      ,{title: '操作',templet: '#barDemo'}
	    ]]
	    ,id: 'testReload'
	    ,page: true
	    ,height: 310
	  });
	  
	//监听工具条
	  table.on('tool(user)', function(obj){
	    var data = obj.data;
	    if(obj.event === 'detail'){
	      layer.msg('ID：'+ data.id + ' 的查看操作');
	    } else if(obj.event === 'del'){
	      layer.confirm('真的删除行么', function(index){
	    	  $.ajax({
	    		  url:'/user/delete',
	    		  method:'post',
	    		  data:{id:data.id},
	    		  dataType:'JSON',
	    		  success:function(res){
	    			  if(res.code==0){
	    				  layer.msg('删除成功',{
	    					  icon:1,
	    					  time:2000
	    				  },function(){
	    					  //刷新数据
	    					  layui.table.reload('testReload',{page:{curr:1}});
	    					  layer.closeAll();
	    				  });
	    			  }else{
	    				  layer.msg(res.msg,{
	    					  icon:2,
	    					  time:1000
	    				  },function(){
	    					  
	    				  });
	    			  }
	    		  }
	    	  });
	      });
	    } else if(obj.event === 'edit'){
	    	layer.open({
				  title:'修改用户信息',
				  type:2,
				  area:['570px','430px'],
				  fix:false,
				  maxmin:true,
				  content:'/user/edit?id='+data.id
			  });
	    }
	  });
	  
	  var $ = layui.$, active = {
	    reload: function(){
	      var demoReload = $('#demoReload');
	      
	      //执行重载
	      table.reload('testReload', {
	        page: {
	          curr: 1 //重新从第 1 页开始
	        }
	        ,where: {
	          username: demoReload.val()
	        }
	      }, 'data');
	    }
	  };
	  
	  $('.demoTable .layui-btn').on('click', function(){
	    var type = $(this).data('type');
	    active[type] ? active[type].call(this) : '';
	  });
	  
	  //添加
	  $('#btn-add').on('click',function(){
		  layer.open({
			  title:'新增用户',
			  type:2,
			  area:['570px','430px'],
			  fix:false,
			  maxmin:true,
			  content:'/user/add'
		  })
	  })
	  
	  //冻结
	  $('#btn-lock').on('click',function(){
		  var checkStatus = table.checkStatus('testReload'); //idTest 即为基础参数 id 对应的值
		  
		  console.log(checkStatus.data) //获取选中行的数据
		  console.log(checkStatus.data.length) //获取选中行数量，可作为是否有选中行的条件
		  console.log(checkStatus.isAll ) //表格是否全选
		  if(checkStatus.data.length==0 || checkStatus.data.length>1){
			  layer.msg("请选择一条数据");
			  return;
		  }
		  layer.confirm('真的要冻结该用户吗', function(index){
	    	  $.ajax({
	    		  url:'/user/lock',
	    		  method:'post',
	    		  data:{id:checkStatus.data[0].id},
	    		  dataType:'JSON',
	    		  success:function(res){
	    			  if(res.code==0){
	    				  layer.msg('操作成功',{
	    					  icon:1,
	    					  time:2000
	    				  },function(){
	    					  //刷新数据
	    					  layui.table.reload('testReload',{page:{curr:1}});
	    					  layer.closeAll();
	    				  });
	    			  }else{
	    				  layer.msg(res.msg,{
	    					  icon:2,
	    					  time:1000
	    				  },function(){
	    					  
	    				  });
	    			  }
	    		  }
	    	  });
	      });
	  });
	  
	  //解冻
	  $('#btn-unlock').on('click',function(){
		  var checkStatus = table.checkStatus('testReload'); //idTest 即为基础参数 id 对应的值
		  if(checkStatus.data.length==0 || checkStatus.data.length>1){
			  layer.msg("请选择一条数据");
			  return;
		  }
		  layer.confirm('真的要解封该用户吗', function(index){
	    	  $.ajax({
	    		  url:'/user/unlock',
	    		  method:'post',
	    		  data:{id:checkStatus.data[0].id},
	    		  dataType:'JSON',
	    		  success:function(res){
	    			  if(res.code==0){
	    				  layer.msg('操作成功',{
	    					  icon:1,
	    					  time:2000
	    				  },function(){
	    					  //刷新数据
	    					  layui.table.reload('testReload',{page:{curr:1}});
	    					  layer.closeAll();
	    				  });
	    			  }else{
	    				  layer.msg(res.msg,{
	    					  icon:2,
	    					  time:1000
	    				  },function(){
	    					  
	    				  });
	    			  }
	    		  }
	    	  });
	      });
	  });
	});
</script>

</body>
</html>
